﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace DnPakExplorer.Controls {
	public partial class CashEquipmentPanelCtrl : ItemSlotPanelCtrl {

		const string XML_PATH = ".\\Data\\cash.xml";

		ItemSlotBox1 mHead;
		ItemSlotBox1 mBody;
		ItemSlotBox1 mLeg;
		ItemSlotBox1 mGlove;
		ItemSlotBox1 mBoots;
		ItemSlotBox1 mWeapon;
		ItemSlotBox1 mWeapon2;
		ItemSlotBox1 mRing1;
		ItemSlotBox1 mRing2;
		ItemSlotBox1 mEaring;
		ItemSlotBox1 mNecklace;
		ItemSlotBox1 mSource;

		ItemSlotBox1 mHeadCash;
		ItemSlotBox1 mBodyCash;
		ItemSlotBox1 mLegCash;
		ItemSlotBox1 mGloveCash;
		ItemSlotBox1 mBootsCash;
		ItemSlotBox1 mWeaponCash;
		ItemSlotBox1 mWeapon2Cash;
		ItemSlotBox1 mRing1Cash;
		ItemSlotBox1 mRing2Cash;
		ItemSlotBox1 mEaringCash;
		ItemSlotBox1 mNecklaceCash;

		ItemSlotBox1 mWing;
		ItemSlotBox1 mTail;
		ItemSlotBox1 mStamp;
		ItemSlotBox1 mFairy;
		ItemSlotBox1 mPet;
		ItemSlotBox1 mPetCloth2;
		ItemSlotBox1 mPetCloth;

		Rectangle mItemDestRect = new Rectangle(10, 10, 50, 50);
		Rectangle mTypeDestRect = new Rectangle(10, 10, 50, 50);

		int mSlotWidth = 200;
		int mSlotHeight = 64;

		public CashEquipmentPanelCtrl() {
			InitializeComponent();

			AddSlot(mHead = new ItemSlotBox1());
			AddSlot(mBody = new ItemSlotBox1());
			AddSlot(mLeg = new ItemSlotBox1());
			AddSlot(mGlove = new ItemSlotBox1());
			AddSlot(mBoots = new ItemSlotBox1());
			AddSlot(mSource = new ItemSlotBox1());
			AddSlot(mWeapon = new ItemSlotBox1());
			AddSlot(mWeapon = new ItemSlotBox1());
			AddSlot(mWeapon2 = new ItemSlotBox1());
			AddSlot(mRing1 = new ItemSlotBox1());
			AddSlot(mRing2 = new ItemSlotBox1());
			AddSlot(mEaring = new ItemSlotBox1());
			AddSlot(mNecklace = new ItemSlotBox1());
			AddSlot(mHeadCash = new ItemSlotBox1());
			AddSlot(mBodyCash = new ItemSlotBox1());
			AddSlot(mLegCash = new ItemSlotBox1());
			AddSlot(mGloveCash = new ItemSlotBox1());
			AddSlot(mBootsCash = new ItemSlotBox1());
			AddSlot(mFairy = new ItemSlotBox1());
			AddSlot(mWeaponCash = new ItemSlotBox1());
			AddSlot(mWeapon2Cash = new ItemSlotBox1());
			AddSlot(mRing1Cash = new ItemSlotBox1());
			AddSlot(mRing2Cash = new ItemSlotBox1());
			AddSlot(mEaringCash = new ItemSlotBox1());
			AddSlot(mNecklaceCash = new ItemSlotBox1());
			AddSlot(mWing = new ItemSlotBox1());
			AddSlot(mTail = new ItemSlotBox1());
			AddSlot(mStamp = new ItemSlotBox1());
			AddSlot(mPet = new ItemSlotBox1());
			AddSlot(mPetCloth = new ItemSlotBox1());
			AddSlot(mPetCloth2 = new ItemSlotBox1());

			mHeadCash.Bounds = new Rectangle(150, 10, mSlotWidth, mSlotHeight);
			mHeadCash.TargetSlotId = EquipmentPanel.IS_CASH_HEAD;

			mBodyCash.Bounds = new Rectangle(150, 70, mSlotWidth, mSlotHeight);
			mBodyCash.TargetSlotId = EquipmentPanel.IS_CASH_BODY;

			mLegCash.Bounds = new Rectangle(150, 130, mSlotWidth, mSlotHeight);
			mLegCash.TargetSlotId = EquipmentPanel.IS_CASH_LEG;

			mGloveCash.Bounds = new Rectangle(150, 190, mSlotWidth, mSlotHeight);
			mGloveCash.TargetSlotId = EquipmentPanel.IS_CASH_HAND;

			mBootsCash.Bounds = new Rectangle(150, 250, mSlotWidth, mSlotHeight);
			mBootsCash.TargetSlotId = EquipmentPanel.IS_CASH_BOOTS;

			mFairy.Bounds = new Rectangle(150, 310, mSlotWidth, mSlotHeight);
			mFairy.TargetSlotId = EquipmentPanel.IS_CASH_FAIRY;

			mWeaponCash.Bounds = new Rectangle(210, 10, mSlotWidth, mSlotHeight);
			mWeaponCash.TargetSlotId = EquipmentPanel.IS_CASH_MAIN_WEAPON;

			mWeapon2Cash.Bounds = new Rectangle(210, 70, mSlotWidth, mSlotHeight);
			mWeapon2Cash.TargetSlotId = EquipmentPanel.IS_CASH_SECONDARY_WEAPON;

			mRing1Cash.Bounds = new Rectangle(210, 130, mSlotWidth, mSlotHeight);
			mRing1Cash.TargetSlotId = EquipmentPanel.IS_CASH_RING1;

			mRing2Cash.Bounds = new Rectangle(210, 190, mSlotWidth, mSlotHeight);
			mRing2Cash.TargetSlotId = EquipmentPanel.IS_CASH_RING2;

			mEaringCash.Bounds = new Rectangle(210, 250, mSlotWidth, mSlotHeight);
			mEaringCash.TargetSlotId = EquipmentPanel.IS_CASH_EARRING;

			mNecklaceCash.Bounds = new Rectangle(210, 310, mSlotWidth, mSlotHeight);
			mNecklaceCash.TargetSlotId = EquipmentPanel.IS_CASH_NECKLACE;

			////////////////////////////////////////////////////////////////////////////

			mWing.Bounds = new Rectangle(290, 10, mSlotWidth, mSlotHeight);
			mWing.TargetSlotId = EquipmentPanel.IS_CASH_WING;

			mTail.Bounds = new Rectangle(290, 70, mSlotWidth, mSlotHeight);
			mTail.TargetSlotId = EquipmentPanel.IS_CASH_TAIL;

			mStamp.Bounds = new Rectangle(290, 130, mSlotWidth, mSlotHeight);
			mStamp.TargetSlotId = EquipmentPanel.IS_CASH_STAMP;

			mPet.Bounds = new Rectangle(290, 190, mSlotWidth, mSlotHeight);
			mPet.TargetSlotId = EquipmentPanel.IS_PET;

			mPetCloth.Bounds = new Rectangle(290, 250, mSlotWidth, mSlotHeight);
			mPetCloth.TargetSlotId = EquipmentPanel.IS_PET_CLOTH1;

			mPetCloth2.Bounds = new Rectangle(290, 310, mSlotWidth, mSlotHeight);
			mPetCloth2.TargetSlotId = EquipmentPanel.IS_PET_CLOTH2;

			double scale = 0.95f;

			mSlotBackground.ImageSource = ItemSlotPanelCtrl.mSlotBackground.ImageSource;
			mSlotBackground.SourceRect = new Rectangle(0, 0, 69, 69);
			mSlotBackground.DestRect = new Rectangle(0, 0, (int)Math.Round(69*scale), (int)Math.Round(69*scale));

			this.ForAllView<ItemSlotBox>(slot => {
				slot.Background = mSlotBackground;
				slot.SeletedImage = mSelectedImage;
				slot.ItemDestRect = new Rectangle((int)Math.Round(4*scale), (int)Math.Round(4*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale));
				slot.TypeImage = GetTypeImage(slot.TargetSlotId, new Rectangle((int)Math.Round(4*scale), (int)Math.Round(4*scale), (int)Math.Round(50*scale), (int)Math.Round(50*scale)));
			});

			if (File.Exists(XML_PATH)) {
				XmlDocument doc = new XmlDocument();
				doc.Load(XML_PATH);
				XmlNodeList list = doc.SelectNodes("/Root/Slot");
				int i = 0;
				this.ForEachView<ItemSlotBox>(slot => {
					if (i<list.Count) {
						XmlAttributeCollection attrs = list[i++].Attributes;
						slot.Bounds = new Rectangle(
							int.Parse(attrs.GetNamedItem("x").InnerText),
							int.Parse(attrs.GetNamedItem("y").InnerText),
							slot.Bounds.Width,
							slot.Bounds.Height
							);
						return false;
					} else
						return true;
				});
			}

		}


		protected override void SaveLayout() {
			XmlDocument doc = new XmlDocument();
			doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", ""));
			XmlElement rootNode = doc.CreateElement("Root");
			doc.AppendChild(rootNode);
			this.ForAllView<ItemSlotBox>(slot => {
				XmlElement slotNode = doc.CreateElement("Slot");
				slotNode.SetAttribute("x", slot.Bounds.X.ToString());
				slotNode.SetAttribute("y", slot.Bounds.Y.ToString());
				rootNode.AppendChild(slotNode);
			});
			doc.Save(XML_PATH);
		}
	}
}
